<template>
    <div class="addProject business-workAdd">
        <div class="addForm zll-form">
            <div class="_top">
                <p>{{healthCheck.deptName}} ，{{healthCheck.jobNumber}} {{healthCheck.employeeName}} ， {{healthCheck.onboardingStatusCn}} </p>
                <p><span class="no_">检疫状态：</span>{{healthCheck.taskStatusCn}}</p>
            </div>
            <div class="tableList">
                <table border="1" v-if="type == 'edit'">				
                    <tr>					
                        <th>检验日期</th>
                        <th>检疫单位</th>
                        <th>检定结果</th>
                        <th>有效期（月）</th>
                        <th>检疫状态</th>
                        <th>录入日期</th>
                        <th>录入人</th>
                    </tr>
                    <tr class="add_Table"  v-for="(item, index) in tableData"  :key="index">
                        <td><el-date-picker
							format="yyyy-MM-dd"
							value-format="yyyy-MM-dd"
							clearable class="text_center"  v-model="item.checkTime" :disabled="type == 'see'" type="date"></el-date-picker></td>
                        <td><el-input v-model="item.checkInstitution" placeholder="" class="text_center" clearable :disabled="type == 'see'"></el-input></td>
                        <td>
                            <el-select v-model="item.checkStatus" placeholder="" class="text_center" clearable :disabled="type == 'see'">
                                <el-option label="合格" value="1"></el-option>
                                <el-option label="不合格" value="2"></el-option>
                            </el-select>
                        </td>
                        <td><el-input v-model="item.mothCount" placeholder="请输入有效期" class="text_center" @input="item.mothCount=item.mothCount.replace(/[^0-9\.]/g,'')" clearable :disabled="type == 'see'"></el-input></td>
                        <td>
                            <el-select v-model="item.taskStatus"  placeholder="" class="text_center" clearable disabled>
                                <el-option label="有效" value="1"></el-option>
                                <el-option label="临期" value="2"></el-option>
                                <el-option label="过期" value="3"></el-option>
                            </el-select>
                        </td>
                        <td><el-date-picker v-model="item.nowTime" clearable class="text_center" disabled type="date"></el-date-picker></td>
                        <td><el-input v-model="item.recorderName" placeholder="" class="text_center" clearable disabled></el-input></td>
                    </tr>
                </table>
                <table border="1" v-else>
                    <tr>																																				
                        <th>检验日期</th>
                        <th>检疫单位</th>
                        <th>检定结果</th>
                        <th>有效期（月）</th>
                        <th>录入日期</th>
                        <th>录入人</th>
                    </tr>
					<tbody v-if=" tableData.length > 0">
						<tr class="add_Table" v-for="(item, index) in tableData"  :key="index">
							<td><p>{{ item.checkTime }}</p></td>
							<td><p>{{ item.checkInstitution }}</p></td>
							<td><p>{{ item.checkStatusCn }}</p></td>
							<td><p>{{ item.mothCount }}</p></td>
							<td><p>{{ item.createTimeFt }}</p></td>
							<td><p>{{ item.recorderName }}</p></td>
						</tr>
					</tbody>
					<tr v-else> 
						<td colspan="6">暂无数据</td>
					</tr>
                </table>
            </div>
        </div>
    </div>
</template>
<script>
import {getStore} from "../../../../plugins/localStore";
export default {
    props: ['editData','type','deptList'],
    data(){
        return {
            addForm: {
				personnelId:'',
				mothCount:'',
				checkUserId:'',
				checkInstitution:'',
				checkTime:'',
				checkStatus:'',
				recorderName:''
            },
			user:{},

			tableData:[{}],

			healthCheck: {
				deptName:'',
				jobNumber:'',
				employeeName:'',
				onboardingStatusCn:'',
				taskStatusCn:'',
				personnelId:'',
				recorderName:''
			},
			count:0,
			rules: {
				checkInstitution: [
					{required: true, message: '请输入检疫机构', trigger: 'blur'},
				],
				checkTime: [
					{required: true, message: '请选择最新检疫日期', trigger: 'change'},
				],
				mothCount: [
					{required: true, message: '请输入有效期', trigger: 'blur'},
				],
				checkStatus: [
					{required: true, message: '请输入检疫结果', trigger: 'change'},
				],
			}
        }
    },
    methods: {

		getDeviceCheckLog() {
			let self = this;
			self.tableData = [];

			if (self.type == 'edit'){
				self.tableData.push({
					checkUserId:'',
					checkInstitution:'',
					checkTime:'',
					checkStatus:'',
					mothCount:'',
				})
			} else{
				self.$http({
					url: '/jinl/healthCheck/getHealthCheckLogList',
					method: "post",
					data: JSON.stringify({id:self.addForm.personnelId}),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp => {
					if (resp.success) {
						if(resp.result){
							console.log(resp.result);
							self.tableData = resp.result;

						}
					}
				});
			}

		},

		submitForm(val,type) {
			let self = this;
            for (let data of self.tableData) {
                if (!data.checkInstitution) {
                    self.$message.warning('请输入检疫机构');
                    return -1;
                } else if (!data.checkTime) {
                    self.$message.warning('请选择最新检疫日期');
                    return -1;
                } else if (!data.mothCount) {
                    self.$message.warning('请输入有效期');
                    return -1;
                } else if (!data.checkStatus) {
                    self.$message.warning('请输入检疫结果');
                    return -1;
                } 
            }
			self.operate()
		},

		operate(){
			let self = this;
			if (this.count == 0) {
				self.count++;
				let param = Object.assign({},self.tableData[0]);
				param.checkUserId = this.user.id;
				param.firstCheck = this.healthCheck.firstCheck;
				param.id = this.healthCheck.id;
				param.personnelId = this.addForm.personnelId;

				self.$http({
					url: '/jinl/healthCheck/uploadNewHealthCheck',
					method: "post",
					data: JSON.stringify(param),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp => {
					if (resp.success) {
						self.$message.success('操作成功');
						self.$emit('refreshData');
						self.$refs["addForm"].clearValidate();
						return;
					}
					self.$message.error(resp.message);
					self.count--;
				});
			}
		},
    },
    mounted(){
		this.user = getStore();
    },

	watch: {
		editData(val) {
			this.addForm.personnelId = val.personnelId;
			this.healthCheck = Object.assign({},val);
			this.getDeviceCheckLog()
		},
	},

    components: {}
}
</script>
<style lang="scss" scoped>
@import "@/style/dialog.scss";
._top {
    display: flex;    justify-content: space-between;
    .no_ {
        color: #9e9e9e;
    }
}
</style>